package day02;

import java.util.Scanner;

import org.junit.Test;

/**
 * 输入一个数学计算表达式，如:1+2 然后输出计算后的结果:1+2=3 这里计算表达式只计算一次即可，可以使用加减乘除任意一个,可以进行小数运算。
 * 
 * @author Bonnie
 *
 */
public class Test07 {
	@Test
	public void test() {
		Scanner console = new Scanner(System.in);
		System.out.print("请输入数学计算表达式：");
		String str = console.nextLine();
		double sum = 0;
		if (str.matches("[\\d|\\.]{1,}[+]{1}[\\d|\\.]{1,}")) {// [\d|\.]{1,}[+]{1}[\d|\.]{1,}
			String[] ss = str.split("[+]{1}");
			sum = Double.parseDouble(ss[0]) + Double.parseDouble(ss[1]);
			System.out.print(ss[0] + "+" + ss[1] + "=" + sum);
		} else if (str.matches("[\\d|\\.]{1,}[-]{1}[\\d|\\.]{1,}")) {
			String[] ss = str.split("[-]{1}");
			sum = Double.parseDouble(ss[0]) - Double.parseDouble(ss[1]);
			System.out.print(ss[0] + "-" + ss[1] + "=" + sum);
		} else if (str.matches("[\\d|\\.]{1,}[/]{1}[\\d|\\.]{1,}")) {
			String[] ss = str.split("[/]{1}");
			sum = Double.parseDouble(ss[0]) / Double.parseDouble(ss[1]);
			System.out.print(ss[0] + "/" + ss[1] + "=" + sum);
		} else if (str.matches("[\\d|\\.]{1,}[*]{1}[\\d|\\.]{1,}")) {
			String[] ss = str.split("[*]{1}");
			sum = Double.parseDouble(ss[0]) * Double.parseDouble(ss[1]);
			System.out.print(ss[0] + "*" + ss[1] + "=" + sum);
		}
		console.close();
	}
}
